/*
 * Copyright (C) 2013 'Chaos'.Personal
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.silly.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.silly.adapter.entity.EntityGrub;
import com.silly.adapter.entity.EntityItem;
import com.silly.adapter.entity.SillyBean;

public class EntityAdapter extends SillyAdapter {

	private Context mContext;

	private List<EntityItem> mEntityList = null;

	private SillyLoader mLoader = null;

	private EntityGrub mGrub = null;

	private LayoutInflater mInflater = null;
    
	private List<SillyBean> mSource = null;

	public EntityAdapter(Context context, List<SillyBean> sbList) {
		this.mContext = context;
		this.mLoader = SillyLoader.get(context);
		this.mGrub = new EntityGrub();
		this.mEntityList = mGrub.grubList(sbList);
		this.mInflater = LayoutInflater.from(context);
		this.mSource = sbList;
		setMultipleType(getViewTypeCount() > 1);
	}

	@Override
	public int getCount() {
		return mEntityList.size();
	}

	@Override
	void bindView(int position, View parent) {

		EntityItem item = mEntityList.get(position);
		final int size = item.size();

		for (int i = 0; i < size; i++) {
			int viewId = item.getViewId(i);
			View v = parent.findViewById(viewId);
			mLoader.setViewObjectList(v, item.getData(viewId), position);
		}
	}

	@Override
	View newView(int position, ViewGroup parent) {
		int layoutId = mEntityList.get(position).getLayoutId();
		return mInflater.inflate(layoutId, parent, false);
	}

	@Override
	public int getItemViewType(int position) {
		return mGrub.getType(position);
	}

	@Override
	public int getViewTypeCount() {
		return mGrub.getTypeCount();
	}

	@Override
	public SillyBean getItem(int position) {
		return mSource.get(position);
	}
    
	@Override
	public long getItemId(int position) {
		return position;
	}

    
	public void refresh(int pos, SillyBean bean) {
        mSource.set(pos, bean);
        EntityItem entity = mGrub.grub(bean);
        mEntityList.set(pos, entity);
        notifyDataSetChanged();
	}
	
	public void refresh(int pos){
		SillyBean bean = mSource.get(pos);
		refresh(pos, bean);
	}
    
	public void refresh(SillyBean bean){
		int index = mSource.indexOf(bean);
		if(index < 0){
			throw new IllegalArgumentException("make sure the bean in the list...");
		}
        System.out.println(" index is ? " + index);
		refresh(index);
	}
    
	public void refresh(SillyBean bean,String field){
		
	}
    
	public void add(SillyBean bean){
		mSource.add(bean);
		mEntityList.add(mGrub.grub(bean));
		notifyDataSetChanged();
	}
    
	public void add(SillyBean bean, int index) {
		mSource.add(index, bean);
		EntityItem entity = mGrub.grub(bean);
		mEntityList.add(index, entity);
		notifyDataSetChanged();
	}
	
	public void remove(int pos){
		mSource.remove(pos);
		mEntityList.remove(pos);
		notifyDataSetChanged();
	}
    
	public void remove(SillyBean bean){
		int index = mSource.indexOf(bean);
		remove(index);
	}

}
